Completed
Push — master ( 7f608b...fc927f )
by Sander
01:15
created

window.contextMenu.constructor   B

Complexity

Conditions 1
Paths 4

Size

Total Lines 115

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
c 2
b 0
f 0
nc 4
nop 0
dl 0
loc 115
rs 8.2857

6 Functions

Rating   Name   Duplication   Size   Complexity  
A window.contextMenu.setContextItems 0 5 1
A contextMenu.js ➔ createMenuItem 0 9 1
A window.contextMenu.setContextItems 0 3 1
B contextMenu.js ➔ initMenus 0 32 1
A contextMenu.js ➔ itemClickCallback 0 22 3
A contextMenu.js ➔ ... ➔ API.tabs.then 0 8 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
/* global API */
2
3
/**
4
 * Nextcloud - passman
5
 *
6
 * @copyright Copyright (c) 2016, Sander Brand ([email protected])
7
 * @copyright Copyright (c) 2016, Marcos Zuriaga Miguel ([email protected])
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
25
window.contextMenu = (function () {
26
    'use strict';
27
    function initMenus() {
28
        API.contextMenus.create({
29
            id: 'autoFill:',
30
            title: 'Auto fill',
31
            contexts: ['page']
32
        });
33
34
        API.contextMenus.create({
35
            id: 'copy:User',
36
            title: 'Copy username',
37
            contexts: ['page']
38
        });
39
40
        API.contextMenus.create({
41
            id: 'copy:Pass',
42
            title: 'Copy password',
43
            contexts: ['page']
44
        });
45
46
47
        API.contextMenus.create({
48
            id: 'copy:Url',
49
            title: 'Copy URL',
50
            contexts: ['page']
51
        });
52
53
        API.contextMenus.create({
54
            id: 'copy:OTP',
55
            title: 'Copy OTP',
56
            contexts: ['page']
57
        });
58
    }
59
60
    function createMenuItem(parentId, id, label, clickcb) {
61
        API.contextMenus.create({
62
            id: id,
63
            title: label,
64
            contexts: ["page"],
65
            parentId: parentId,
66
            onclick: clickcb
67
        });
68
    }
69
70
    function itemClickCallback(menu_action, login) {
71
        var action = menu_action.menu.split(':', 1)[0];
72
73
        if (action === 'copy') {
74
75
            API.tabs.query({active: true, currentWindow: true}).then(function (tabs) {
76
                var text = login[menu_action.field];
77
                if(menu_action.menu.indexOf('OTP') !== -1){
78
                    window.OTP.secret = login.totp;
79
                    text =  window.OTP.getOTP();
80
                }
81
                API.tabs.sendMessage(tabs[0].id, {method: "copyTextToClipboard", args: text});
82
            });
83
            return;
84
        }
85
86
        if (action === 'autoFill') {
87
            API.tabs.query({active: true, currentWindow: true}).then(function (tabs) {
88
                API.tabs.sendMessage(tabs[0].id, {method: "enterLoginDetails", args: login});
89
            });
90
        }
91
    }
92
93
94
    API.contextMenus.removeAll();
95
    initMenus();
96
97
    return {
98
        setContextItems: function (logins) {
99
            var i,f, field;
100
            var fields = [
101
                {menu: 'autoFill:', field: 'autoFill', found: false},
102
                {field: 'username', menu: 'copy:User', found: false},
103
                {field: 'password', menu: 'copy:Pass', found: false},
104
                {field: 'url', menu: 'copy:Url', found: false},
105
                {field: 'totp', menu: 'copy:OTP', found: false}
106
            ];
107
            API.contextMenus.removeAll();
108
            initMenus();
109
110
            for (i = 0; i < logins.length; i++) {
111
                var login = logins[i];
112
                login.autoFill = (!login.hasOwnProperty('autoFill')) ? true : login.autoFill;
113
                for (f = 0; f < fields.length; f++) {
114
                    field = fields[f];
115
                    if (field['field'] === 'totp' && login.otp) {
116
                        login.totp = login.otp.secret;
117
                    }
118
                    if (login[field['field']]) {
119
                        fields[f].found = true;
120
                        createMenuItem(field['menu'], field['menu'] + ':' + login.guid, login.label, (function (field, login) {
121
                            return function () {
122
                                itemClickCallback(field, login);
123
                            };
124
                        })(field, login));
125
                    }
126
                }
127
            }
128
129
            for (f = 0; f < fields.length; f++) {
130
                field = fields[f];
131
                if(field.found === false){
132
                   API.contextMenus.remove(field.menu);
133
                }
134
            }
135
136
        }
137
    };
138
139
}());